import { Body, Controller, Post, Headers, Get, Req } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { SysDictionaryService } from './dictionary.service';
import { ADMIN_PREFIX } from '../../admin.constants';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import { PermissionOptional } from '../../core/decorators/permission-optional.decorator';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('字典表模块')
@Controller('dictionary')
export class SysDictionaryController {
  constructor(private dictionaryService: SysDictionaryService) {}

  @ApiOperation({ summary: '获取获取字典表类型列表' })
  @ApiOkResponse()
  @Get('typeList')
  async typeList() {
    return await this.dictionaryService.typeList();
  }

  @ApiOperation({ summary: '分页获取获取字典表类型列表' })
  @ApiOkResponse()
  @Post('typePage')
  async typePage(@Body() dto) {
    const [list, total] = await this.dictionaryService.typePage(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加字典表类型' })
  @ApiOkResponse()
  @Post('typeAdd')
  async typeAdd(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.dictionaryService.isExistTypeKey(dto.dictionaryTypeCode);

    await this.dictionaryService.typeAdd(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '更新字典表类型' })
  @ApiOkResponse()
  @Post('typeUpdate')
  async typeUpdate(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.dictionaryService.typeUpdate(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除字典表类型' })
  @ApiOkResponse()
  @Post('typeDelete')
  async typeDelete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.dictionaryService.typeDelete(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '获取字典表类型详情' })
  @ApiOkResponse()
  @Post('typeInfo')
  async typeInfo(@Body() dto) {
    return await this.dictionaryService.typeInfo(dto);
  }

  @ApiOperation({ summary: '分页获取获取字典表列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.dictionaryService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加字典表' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    
    await this.dictionaryService.isExistKey(dto);

    await this.dictionaryService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '更新字典表' })
  @ApiOkResponse()
  @Post('update')
  async update(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.dictionaryService.update(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除字典表' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.dictionaryService.delete(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '获取字典表详情' })
  @ApiOkResponse()
  @PermissionOptional()
  @Post('info')
  async info(@Body() dto, @Headers('language') languageType) {
    return await this.dictionaryService.info(dto, languageType);
  }

  @ApiOperation({ summary: '获取字典表字段列表' })
  @ApiOkResponse()
  @Get('cloums')
  async cloums() {
    return await this.dictionaryService.getColumnsList();
  }
}
